package com.hatricks.is.json;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @packageName: com.hatricks.json
 * @className: JsonTest
 * @author: yanyuhui yanyuhui@chinasofti.com
 * @date: 2022/6/20 19:46
 * @description:
 */
public class JsonTest {
    static String JSON = "{\"exeSeqId\":\"1655735493234\",\"job\":{\"cron\":false,\"extParams\":{\"job.detail.po\":\"{\\\"hostPo\\\":[{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.193\\\",\\\"hostName\\\":\\\"target001\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"AlxUnBOA##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"},{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.199\\\",\\\"hostName\\\":\\\"target007\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"wa0D7kS3##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"},{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.201\\\",\\\"hostName\\\":\\\"target006\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"iBCcpNRG##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"},{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.198\\\",\\\"hostName\\\":\\\"target005\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"6iZJzhHK##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"},{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.202\\\",\\\"hostName\\\":\\\"target004\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"fE5xFKx6##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"}],\\\"id\\\":\\\"yjM9tJjt\\\",\\\"inputParam\\\":{},\\\"needOutput\\\":true,\\\"needStdOut\\\":true,\\\"outputParam\\\":{\\\"outputPath\\\":\\\"/okm:root/L63iqUbA/taskOutputParam/b2OrbtRi\\\"},\\\"params\\\":[],\\\"runnerName\\\":\\\"script\\\",\\\"script\\\":\\\"/okm:root/L63iqUbA/taskScript/b2OrbtRi/b2OrbtRi_4008.sh#v1.0#\\\",\\\"skip\\\":false,\\\"sudo\\\":false,\\\"timeout\\\":5000,\\\"workDir\\\":\\\"~\\\"}\"},\"maxRetryTimes\":0,\"needFeedback\":true,\"pipeline\":false,\"priority\":100,\"relyOnPrevCycle\":true,\"repeatCount\":0,\"repeatInterval\":0,\"repeatable\":false,\"replaceOnExist\":false,\"submitNodeGroup\":\"test_jobClient\",\"taskId\":\"b2OrbtRi@@0\",\"taskTrackerNodeGroup\":\"test_trade_TaskTracker\",\"triggerTime\":1655735493234},\"msg\":\"{\\\"yjM9tJjt_6iZJzhHK##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655735499042,\\\"startTime\\\":1655735494662},\\\"yjM9tJjt_fE5xFKx6##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655735500074,\\\"startTime\\\":1655735494713},\\\"yjM9tJjt_iBCcpNRG##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655735498009,\\\"startTime\\\":1655735494611},\\\"yjM9tJjt_AlxUnBOA##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655735495943,\\\"startTime\\\":1655735494469},\\\"yjM9tJjt_wa0D7kS3##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655735496976,\\\"startTime\\\":1655735494539}}\",\"success\":true,\"time\":1655735500087}";

    static String JSON2 = " {\"exeSeqId\":\"1655776684872\",\"job\":{\"cron\":false,\"extParams\":{\"job.detail.po\":\"{\\\"hostPo\\\":[{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.193\\\",\\\"hostName\\\":\\\"target001\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"AlxUnBOA##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"},{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.199\\\",\\\"hostName\\\":\\\"target007\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"wa0D7kS3##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"},{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.201\\\",\\\"hostName\\\":\\\"target006\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"iBCcpNRG##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"},{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.198\\\",\\\"hostName\\\":\\\"target005\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"6iZJzhHK##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"},{\\\"authMode\\\":1,\\\"hostIp\\\":\\\"172.27.134.202\\\",\\\"hostName\\\":\\\"target004\\\",\\\"hostPort\\\":22,\\\"id\\\":\\\"fE5xFKx6##0\\\",\\\"password\\\":\\\"Pass1234\\\",\\\"properties\\\":{},\\\"user\\\":\\\"root\\\"}],\\\"id\\\":\\\"4Q1dlDs3\\\",\\\"inputParam\\\":{},\\\"needOutput\\\":true,\\\"needStdOut\\\":true,\\\"outputParam\\\":{\\\"outputPath\\\":\\\"/okm:root/L63iqUbA/taskOutputParam/b2OrbtRi\\\"},\\\"params\\\":[],\\\"runnerName\\\":\\\"script\\\",\\\"script\\\":\\\"/okm:root/L63iqUbA/taskScript/b2OrbtRi/b2OrbtRi_4008.sh#v1.0#\\\",\\\"skip\\\":false,\\\"sudo\\\":false,\\\"timeout\\\":5000,\\\"workDir\\\":\\\"~\\\"}\"},\"maxRetryTimes\":0,\"needFeedback\":true,\"pipeline\":false,\"priority\":100,\"relyOnPrevCycle\":true,\"repeatCount\":0,\"repeatInterval\":0,\"repeatable\":false,\"replaceOnExist\":false,\"submitNodeGroup\":\"test_jobClient\",\"taskId\":\"b2OrbtRi@@0\",\"taskTrackerNodeGroup\":\"test_trade_TaskTracker\",\"triggerTime\":1655776684872},\"msg\":\"{\\\"4Q1dlDs3_wa0D7kS3##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655776687370,\\\"startTime\\\":1655776685114},\\\"4Q1dlDs3_AlxUnBOA##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655776686334,\\\"startTime\\\":1655776685049},\\\"4Q1dlDs3_6iZJzhHK##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655776689438,\\\"startTime\\\":1655776685223},\\\"4Q1dlDs3_fE5xFKx6##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655776690472,\\\"startTime\\\":1655776685284},\\\"4Q1dlDs3_iBCcpNRG##0\\\":{\\\"action\\\":0,\\\"endTime\\\":1655776688405,\\\"startTime\\\":1655776685165}}\",\"success\":true,\"time\":1655776690475}\n";


    public static void main(String[] args) {
        parseCountTime(JSON);
        parseCountTime(JSON2);
    }

    private static void parseCountTime(String json) {
        JSONObject jsonObject = JSONUtil.parseObj(json);
        JSONObject msg = jsonObject.getJSONObject("msg");
        Map<String, Long> result = new HashMap<>();
        long time = 0;
        for (Map.Entry<String, Object> node : msg.entrySet()) {
            JSONObject nodeV = JSONUtil.parseObj(node.getValue());
            Long startTime = nodeV.getLong("startTime");
            Long endTime = nodeV.getLong("endTime");
            long sub = DateUtil.betweenMs(DateUtil.date(startTime), DateUtil.date(endTime));
            time += sub;
            result.put(node.getKey(), sub);
        }
        System.out.println(result);
        System.out.println(StrUtil.format("总共机器数量为:{},总耗时为:{}", result.size(), time));
    }
}
